package com.atguigu.rest.controller;

import com.atguigu.rest.dao.EmployeeDao;
import com.atguigu.rest.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @author 陆小根
 * date: 2022/05/01
 * Description:
 */
@Controller
public class EmployeeController {

  private final EmployeeDao employeeDao;

  @Autowired
  public EmployeeController(EmployeeDao employeeDao) {
    this.employeeDao = employeeDao;
  }

  /*获取所有员工*/
  @GetMapping("/employee")
  public String getAllEmployee(Model model) {
    Collection<Employee> employeeList = employeeDao.getAll();
    model.addAttribute("employeeList", employeeList);
    return "employee_list";
  }

  /*删除员工*/
  @DeleteMapping(value = "/employee/{id}")
  public String deleteEmployee(@PathVariable("id") Integer id) {
    employeeDao.delete(id);
    return "redirect:/employee";
  }

  /*添加员工*/
  @PostMapping("/employee")
  public String addEmployee(Employee employee) {
    employeeDao.save(employee);
    return "redirect:/employee";
  }

  /*回显员工信息*/
  @GetMapping("/employee/{id}")
  public String getEmployeeById(@PathVariable("id") Integer id, Model model) {
    Employee employee = employeeDao.get(id);
    model.addAttribute("employee", employee);
    return "employee_update";
  }

  /*更新员工信息*/
  @PutMapping("/employee")
  public String updateEmployee(Employee employee) {
    employeeDao.save(employee);
    return "redirect:/employee";
  }

}
